<template>
  <div class="list-page">
    <Row>
      <div class="toolbar">
        <Input
          placeholder="请输入要查询的设备序列号"
          v-model="bloc.form.deviceSn"
          style="width: 205px; margin-right: 10px"
        />
        <Input
          placeholder="请输入要查询的联系人"
          v-model="bloc.form.name"
          style="width: 180px; margin-right: 10px"
        />
        <Input
          placeholder="请输入要查询的联系电话"
          v-model="bloc.form.phone"
          style="width: 180px; margin-right: 10px"
        />
        <DatePicker
          type="daterange"
          placeholder="添加时间"
          format="yyyy/MM/dd"
          v-model="bloc.form.date"
          :editable="false"
          style="width: 200px; margin-right: 10px"
        ></DatePicker>
        <Button
          type="primary"
          icon="ios-search"
          style="margin: 0 10px"
          size="default"
          @click="bloc.load(1, bloc.paged.pageSize)"
        >查询
        </Button>
        <Button icon="md-refresh" @click="bloc.reset()">重置</Button>
      </div>
    </Row>
    <Row>
      <Table
        border
        :loading="bloc.loading"
        :columns="columns"
        highlight-row
        :data="bloc.paged.items"
      >
        <template slot-scope="{ row }" slot="action">
          <div style="white-space: nowrap">
            <Button
              size="small"
              style="margin-left: 5px; margin-right: 5px"
              @click="detailItem(row)"
              type="info"
              v-if="Validation.check(Resources.POSTSALE)"
            >详情
            </Button>
          </div>
        </template>
        <slot name="footer" slot="footer">
          <Page
            show-elevator
            show-total
            show-sizer
            :total="bloc.paged.itemCount"
            :current="bloc.paged.pageIndex"
            :page-size="bloc.paged.pageSize"
            :page-size-opts="[15, 30, 50, 100]"
            @on-change="(val) => bloc.load(val, bloc.paged.pageSize)"
            @on-page-size-change="(val) => bloc.load(bloc.paged.pageIndex, val)"
          />
        </slot>
      </Table>
    </Row>
  </div>
</template>

<script lang="ts">
    import { Component, Vue } from "vue-property-decorator";
    import { Route } from "vue-router";
    import { ApplyBloc } from "@/view/service/apply/ApplyBloc";
    import { MainTain } from "@/base/model/MainTain";
    import { formatDate } from "@/base/util/Dates";
    import CompanySelector from "@/component/CompanySelector.vue";
    import { Validation } from "@/base/Validation";
    import { Resources } from "@/base/Resources";

    const columns = (bloc: ApplyBloc) => [
        // {
        //     title: "设备来源",
        //     key: "deviceFrom",
        //     align: "left",
        //     minWidth: 200,
        //     tooltip: true,
        // },
        // {
        //     title: "设备分类",
        //     key: "deviceSort",
        //     align: "left",
        //     minWidth: 200,
        //     tooltip: true,
        // },
        {
            title: "设备型号",
            key: "PM",
            align: "left",
            minWidth: 200,
            tooltip: true,
        },
        {
            title: "设备序列号",
            key: "SN",
            align: "left",
            minWidth: 200,
            tooltip: true,
        },
        {
            title: "购买时间",
            render: (h: any, params: any) => {
                return h("span", {
                    domProps: {
                        innerText: formatDate(
                            new Date(params.row.buyTime),
                            "yyyy-MM-dd"
                        ),
                    },
                });
            },
            width: 160,
            align: "left",
            tooltip: true,
        },
        {
            title: "申请时间",
            render: (h: any, params: any) => {
                return h("span", {
                    domProps: {
                        innerText: formatDate(
                            new Date(params.row.applyTime),
                            "yyyy-MM-dd hh:mm:ss"
                        ),
                    },
                });
            },
            width: 160,
            align: "left",
            tooltip: true,
        },
        {
            title: "联系人",
            key: "name",
            align: "left",
            minWidth: 200,
            tooltip: true,
        },
        {
            title: "联系电话",
            key: "tel",
            align: "left",
            minWidth: 200,
            tooltip: true,
        },
        {
            title: "维修进度",
            key: "state",
            render: (h: any, params: any) => {
                return h("span", {
                    domProps: {
                        innerText: params.row.state === "0" ? '已取消'
                            :params.row.state === "1" ||　params.row.state === "2"?'处理中'
                                :'已完成'
                    }
                });
            },
            align: "left",
            minWidth: 200,
            tooltip: true,
        },
        {
            title: "操作",
            slot: "action",
            width: 47 * 2 + 20,
            align: "left",
            fixed: "right",
        },
    ];

    @Component
    export default class extends Vue {
        beforeRouteEnter(to: Route, from: Route, next: (vm: any) => void) {
            next((vm) => {
                vm.bloc
                    .load(vm.bloc.paged.pageIndex, vm.bloc.paged.pageSize)
                    .catch((e) => {
                        vm.$Message.error("操作错误");
                    });
            });
        }

        bloc: ApplyBloc = new ApplyBloc();
        Validation = Validation;
        Resources = Resources;
        columns = columns(this.bloc);


        detailItem(dataItem: MainTain) {
            const num:string = JSON.stringify(dataItem);
            this.$router.push({
                path:`/apply/detail`,
                query:{param:num}});
        }
    }
</script>

<style lang="less">
</style>
